package com.bmps.springcloud.insuredmicroservice.service.underwrite.strategy.insurance;

import com.bmps.springcloud.common.utils.DateUtil;
import com.bmps.springcloud.insuredmicroservice.config.accept.ProductConfig;
import com.bmps.springcloud.insuredmicroservice.constant.accept.AcceptConstant;
import com.bmps.springcloud.insuredmicroservice.constant.accept.UnderWriteConstant;
import com.bmps.springcloud.insuredmicroservice.model.accept.resquest.*;
import com.bmps.springcloud.insuredmicroservice.model.underwrite.InsuredModel;
import com.bmps.springcloud.insuredmicroservice.model.underwrite.RiskModel;
import com.bmps.springcloud.insuredmicroservice.repository.underwrite.coresystem.InsuredCoreRepository;
import com.bmps.springcloud.insuredmicroservice.util.accept.AcceptUtil;
import org.apache.commons.lang3.StringUtils;

import java.util.Date;
import java.util.List;

/**
 * @author : Jacob
 * @date : 2020/2/10 13:31
 **/
public class Aat046InsuranceStrategy implements InsuranceStrategy {
    @Override
    public InsuredModel checkInsuredRolesByRiskCode(AcceptRequestDataModel acceptRequestDataModel, InsuredCoreRepository insuredCoreRepository, ProductModel productModel, ProductConfig productConfig) {
        RequestModel requestModel = acceptRequestDataModel.getRequest();
        CustInfoModel custInfoModel = requestModel.getCustInfo();
        PolicyInfoModel policyInfoModel = requestModel.getPolicyInfo();

        Date valiDate = requestModel.getPolicyInfo().getCvalidate();

        //投保人职业类别
        if (StringUtils.isEmpty(custInfoModel.getAppnt().getOccupationType())) {
            return AcceptUtil.getInsuredResponse(UnderWriteConstant.UNDER_WRITE_FIAL_CODE, UnderWriteConstant.UNDER_WRITE_NUGG0026_MESSAGE);
        }
        // 职业类别，product配置文件不适合
        String [] occupationTypesList = {"1","2","3","4","5","6"};
        //被保人信息
        List<InsurantModel> insurantModelList = custInfoModel.getInsurants().getInsurant();
        for (InsurantModel insurantModel : insurantModelList) {
            Date birthday = insurantModel.getBirthday();
            if (insurantModel.getInsSeq() == productModel.getProdInsSeq()) {
                //获取日期相差的天数
                int years = DateUtil.getYears(birthday, valiDate);

                String type = insurantModel.getOccupationType();
                //与投保人的关系
                String relation = insurantModel.getRelToApp();
                //1.校验职业类别是否填写
                if (StringUtils.isEmpty(type)) {
                    return AcceptUtil.getInsuredResponse(UnderWriteConstant.UNDER_WRITE_FIAL_CODE, UnderWriteConstant.UNDER_WRITE_NPGG0017_MESSAGE);
                }
                //3.被保人理赔标识
                if (insurantModel.getClaimFlag() > 0) {
                    return AcceptUtil.getInsuredResponse(UnderWriteConstant.UNDER_WRITE_NUGG0027,
                            String.format(UnderWriteConstant.UNDER_WRITE_NUGG0027_MESSAGE,insurantModel.getName()));
                }
                boolean flag = false;
                for (String occupation: occupationTypesList){
                    if (type.equals(occupation)){
                        flag = true;
                    }
                }
                if (!flag) {
                    return AcceptUtil.getInsuredResponse(UnderWriteConstant.UNDER_WRITE_FIAL_CODE, String.format(UnderWriteConstant.UNDER_WRITE_NPGG0016_MESSAGE,type));
                }

                // 被保人为未成年，投被保人关系只能为父母
                if (!AcceptConstant.PARENTAL_RELATIONSHIP.contains(relation) && UnderWriteConstant.AGE_EIGHTEEN_YEAR > years){
                    return AcceptUtil.getInsuredResponse(UnderWriteConstant.UNDER_WRITE_FIAL_CODE,
                            UnderWriteConstant.UNDER_WRITE_NUGG0041_MESSAGE);
                }
                //2.校验投保人和被保人的关系
                if (!(productConfig.getRelationList().contains(relation))) {
                    return AcceptUtil.getInsuredResponse(UnderWriteConstant.UNDER_WRITE_FIAL_CODE,
                            UnderWriteConstant.UNDER_WRITE_NUGG0007_MESSAGE);
                }
                //5.黑名单
                if (insurantModel.getBlackListFlag() > 0) {
                    return AcceptUtil.getInsuredResponse(UnderWriteConstant.UNDER_WRITE_NUGG0031,
                            String.format(UnderWriteConstant.UNDER_WRITE_NUGG0031_MESSAGE,insurantModel.getName()));
                }
            }
        }

        int prodInsSeq = productModel.getProdInsSeq();
        String prtNo = policyInfoModel.getPrtNo();
        for (InsurantModel insurantModel : insurantModelList) {
            if (insurantModel.getInsSeq() == prodInsSeq) {
                List<RiskModel> result = insuredCoreRepository.getInsuranceCountLimOne(productModel.getProdCode(), prtNo, insurantModel.getInsuredNo());
                if (result.size()>=1){
                    return AcceptUtil.getInsuredResponse(UnderWriteConstant.UNDER_WRITE_FIAL_CODE, UnderWriteConstant.UNDER_WRITE_LIMIt_ONE_NPGG0032_MESSAGE);
                }
            }
        }
        return AcceptUtil.getInsuredResponse(AcceptConstant.RESULT_SUCCESS_CODE, AcceptConstant.RESULT_SUCCESS_MESSAGE);

    }
}
